package com.yygh.hosp.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yygh.common.exception.YyghException;
import com.yygh.common.result.Result;
import com.yygh.common.util.MD5;
import com.yygh.hosp.service.HospitalSetService;
import com.yygh.model.hosp.HospitalSet;
import com.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院管理设置")
@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @GetMapping("/findall")
    @ApiOperation("显示所有医院信息")
    public Result getall(){
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除医院信息")
    private Result dell(@PathVariable long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag){
          return Result.ok();
        }else {
            return Result.fail();
        }
    }

   //3 条件查询带分页
    @ApiOperation("分页条件查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPgeHospSet(@PathVariable long current, @PathVariable long limit,
                                @RequestBody(required = false) HospitalSetQueryVo hsvo){
        //long currents=current*limit+1;
        Page<HospitalSet> resultPage = new Page<>(current, limit);
        String hosname = hsvo.getHosname();
        String hoscode = hsvo.getHoscode();

        LambdaQueryWrapper<HospitalSet> lqw = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(hosname)){
            lqw.like(HospitalSet::getHosname,hsvo.getHosname());
        }

        if(!StringUtils.isEmpty(hoscode)){
            lqw.eq(HospitalSet::getHoscode,hsvo.getHoscode());
        }

        Page<HospitalSet> page = hospitalSetService.page(resultPage, lqw);
        List<HospitalSet> records = page.getRecords();
        System.out.println(records);

        return Result.ok(page);
    }
    //新增医院信息
    @ApiOperation("新增医院信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);

        Random random = new Random();
        String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(encrypt);

        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }
    //5 根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable long id){
        HospitalSet result = hospitalSetService.getById(id);
        try {
            int i=1;
           // int x=i/0;
        }catch (RuntimeException e){
            throw new YyghException("test",201);
        }

        if(result!=null){
            return Result.ok(result);
        }else {
            return Result.fail();
        }
    }


    //6 修改医院设置
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //7 批量删除医院设置
    @DeleteMapping("batchRemove")
    @ApiOperation("批量删除医院设置")
    public Result batchRemove(@RequestBody List<Long> ids){
        boolean flag = hospitalSetService.removeByIds(ids);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //8 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable long id, @PathVariable int status){
        HospitalSet hosp = hospitalSetService.getById(id);
        hosp.setStatus(status);
        hospitalSetService.updateById(hosp);
        return Result.ok();
    }

    //9 发送签名秘钥
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }
}
